from handlers.base import BaseHandler
from tornado.web import asynchronous
from tornado.gen import coroutine
import json
import time

class InfoHandler(BaseHandler):
	@asynchronous
	@coroutine
	def get(self):
		if(self.session['current_session']):
			email = self.session['current_session']
			res = yield self.db.user.find_one({'email':email})
			uname = res["uname"]
			data = {
				"email": email,
				"uname": uname
			}
			return self.write(data)
		return self.write("error")

	@asynchronous
	@coroutine
	def post(self):
		data = self.get_argument("user")
		data = json.loads(data)
		re = yield self.db.user.update({'email':data['email']},{
			'$set': {
				'uname': data['uname']
			}
		})
		if(re):
			return self.write("success")
		return self.write("error")

class PasswdHandler(BaseHandler):
	@asynchronous
	@coroutine
	def post(self):
		data = self.get_argument("data")
		data = json.loads(data)
		
		res = yield self.db.user.find_one({'passwd':data['oldPwd']})